
<?php

/**
 * A view helper that displays a link for sorting the column
 * Used for sorting a table of information
 * @todo Make navigation arrow part of a class?
 */

class Zend_View_Helper_SortOrder extends Zend_View_Helper_Abstract
{

	const DIRECTION_PARAM = 'direction';
	const ORDER_PARAM = 'order';
	const DIRECTION_ASC = 'ASC';
	const DIRECTION_DESC = 'DESC';

	/**
     * Return the html code to have the header of the column sortable
	 *
     * @param string $sFieldName : name of the field on which order by must be effected
     * @param string $sDisplayName : name to display in the header of the column
     * @return string
     */
	public function sortOrder($sFieldName, $sDisplayName)
	{
		$oRequest = Zend_Controller_Front::getInstance()->getRequest();
		$sOrderBy = $oRequest->getParam(self::ORDER_PARAM);
		$sDirection = $oRequest->getParam(self::DIRECTION_PARAM);

		if ($sFieldName === $sOrderBy)
		{
			switch($sDirection)
			{
				case self::DIRECTION_ASC:
					$sDirection = self::DIRECTION_DESC;
					$sArrow = '&uarr;';
					break;
				case self::DIRECTION_DESC:
					$sDirection = self::DIRECTION_ASC;
					$sArrow = '&darr;';
					break;
			}
		} else {
			$sDirection = self::DIRECTION_ASC;
			$sArrow = null;
		}

		$output = '<a href="';
		$output .= $this->view->getHelper('Url')->url(array(
			self::ORDER_PARAM => $sFieldName,
			self::DIRECTION_PARAM => $sDirection
		));
		$output .= '">'. $sDisplayName . '</a>' . $sArrow;

		return $output;
	}

}
